package com.lonely.fx;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;

import java.io.File;

/**
 * @date: 2022-07-25 14:44
 * @author: ztkj-hzb
 * @description: 目录选择器
 **/
public class DirectoryChooserSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        VBox vBox = new VBox();
        vBox.setSpacing(30);
        vBox.setPadding(new Insets(20));

        Button button = new Button("打开目录");

        TextArea textArea = new TextArea();
        textArea.prefWidthProperty().bind(vBox.widthProperty());


        button.setOnAction(event -> {

            DirectoryChooser directoryChooser = new DirectoryChooser();

            //设置初始化目录
            directoryChooser.setInitialDirectory(new File("D:/"));

            //设置标题，默认是 选择文件夹
            directoryChooser.setTitle("请选择文件夹");

            //选择目录，返回目录对象
            File file = directoryChooser.showDialog(primaryStage);

            if (file == null) {
                return;
            }

            File[] files = file.listFiles();
            for (File currentFile : files) {
                textArea.appendText(currentFile.getAbsolutePath() + "\r\n");
            }

        });


        vBox.getChildren().addAll(button, textArea);
        primaryStage.setScene(new Scene(vBox));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();
    }
}
